#### Installing and requiring packages####
install.packages("simcf") # this needs to be done manually, package available here http://faculty.washington.edu/cadolph/?page=60
install.packages("plm")
install.packages("pcse")
install.packages("stats")
install.packages("gmodels")
install.packages("repmis")

library(plm)
library(pcse)
library(simcf)
library(stats)
library(gmodels)
library(repmis)

####Acquiring and viewing data####
replication_states <- repmis::source_DropboxData("replication_states.csv", "bhdeskajeeiewvl", sep = ",", header = TRUE)
replication_gss <- repmis::source_DropboxData("replication_gss.csv", "nhe1tdtios1nvrk", sep = ",", header = TRUE)
replication_nes <- repmis::source_DropboxData("replication_nes.csv", "xprnfbfb2x7fu79", sep = ",", header = TRUE)

View(replication_states)
View(replication_gss)
View(replication_nes)
head(replication_gss)
head(replication_nes)
summary(replication_gss)
summary(replication_nes)
head(replication_nes)
nrow(replication_gss)

###############################################
#TABLE 1#####
###############################################

#Agree that income differentials are too big####

replication_gss$incgap2 <- replication_gss$incgap
replication_gss$incgap2[replication_gss$incgap2 == "2"] <- "1"
library(gmodels)
CrossTable(replication_gss$incgap2, replication_gss$incomeclass, expected=FALSE, prop.r=FALSE, prop.c=TRUE,
           prop.t=FALSE, prop.chisq=FALSE, chisq = FALSE, fisher=FALSE, mcnemar=FALSE,
           resid=FALSE, sresid=FALSE, asresid=FALSE,
           missing.include=FALSE,
           format=c("SAS"), dnn = NULL)


#Agree that government should reduce income differentials####

replication_gss$goveqinc2  <- replication_gss$goveqinc
replication_gss$goveqinc2[replication_gss$goveqinc2 == "2"] <- "1"
CrossTable(replication_gss$goveqinc2, replication_gss$incomeclass, expected=FALSE, prop.r=FALSE, prop.c=TRUE,
           prop.t=FALSE, prop.chisq=FALSE, chisq = FALSE, fisher=FALSE, mcnemar=FALSE,
           resid=FALSE, sresid=FALSE, asresid=FALSE,
           missing.include=FALSE,
           format=c("SAS"), dnn = NULL)

#Voted for Obama####

CrossTable(replication_nes$V085044a, replication_nes$incomeclass)
CrossTable(replication_nes$V085044a, replication_nes$incomeclass, expected=FALSE, prop.r=FALSE, prop.c=TRUE,
           prop.t=FALSE, prop.chisq=FALSE, chisq = FALSE, fisher=FALSE, mcnemar=FALSE,
           resid=FALSE, sresid=FALSE, asresid=FALSE,
           missing.include=FALSE,
           format=c("SAS"), dnn = NULL)

#Democratic Partisanship#####

replication_nes$Dem_part <- replication_nes$V083098x
replication_nes$Dem_part[replication_nes$Dem_part == "0" | replication_nes$Dem_part == "1" |replication_nes$Dem_part == "2" ] <- "1"
CrossTable(replication_nes$Dem_part, replication_nes$incomeclass, expected=FALSE, prop.r=FALSE, prop.c=TRUE,
           prop.t=FALSE, prop.chisq=FALSE, chisq = FALSE, fisher=FALSE, mcnemar=FALSE,
           resid=FALSE, sresid=FALSE, asresid=FALSE,
           missing.include=FALSE,
           format=c("SAS"), dnn = NULL)

